package com.ydlclass.channelHandler.handler;

import com.ydlclass.YrpcBootstrap;
import com.ydlclass.enumeration.RespCode;
import com.ydlclass.exceptions.ResponseException;
import com.ydlclass.protection.CircuitBreaker;
import com.ydlclass.transport.message.YrpcResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 这是一个用来测试的类
 */
@Slf4j
public class MySimpleChannelInboundHandler extends SimpleChannelInboundHandler<YrpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, YrpcResponse yrpcResponse) throws Exception {
        //从全局的挂起的请求中寻找与之相匹配的待处理的 cf
        CompletableFuture<Object> completableFuture = YrpcBootstrap.PENDING_REQUEST.get(yrpcResponse.getRequestId());
        SocketAddress socketAddress = channelHandlerContext.channel().remoteAddress();
        Map<SocketAddress, CircuitBreaker> everyIpCircuitBreaker =
                YrpcBootstrap.getInstance().getConfiguration().getEveryIpCircuitBreaker();
        CircuitBreaker circuitBreaker = everyIpCircuitBreaker.get(socketAddress);
        byte code = yrpcResponse.getCode();
        if(code == RespCode.FAIL.getCode()){
            circuitBreaker.recordErrorRequest();
            completableFuture.complete(null);
            log.error("当前id为{}的请求,返回错误的结果,响应码为{}",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            throw new ResponseException(code,RespCode.FAIL.getDesc());
        }else if (code == RespCode.RATE_LIMIT.getCode()){
            circuitBreaker.recordErrorRequest();
            completableFuture.complete(null);
            log.error("当前id为{}的请求,被限流,响应码为{}",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            throw new ResponseException(code,RespCode.RATE_LIMIT.getDesc());
        }else if (code == RespCode.RESOURCE_NOT_FOUND.getCode()){
            circuitBreaker.recordErrorRequest();
            completableFuture.complete(null);
            log.error("当前id为{}的请求,未找到目标资源,响应码为{}",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            throw new ResponseException(code,RespCode.RESOURCE_NOT_FOUND.getDesc());
        }else if (code == RespCode.SUCCESS.getCode()){
            //服务提供方，给予的结果
            Object returnValue = yrpcResponse.getBody();
            // todo 需要针对code做处理
            returnValue = returnValue == null ? new Object() : returnValue;
            completableFuture.complete(returnValue);
            if(log.isDebugEnabled()){
                log.debug("已经在消费端，找到编号为{}的completableFuture，处理响应结果.",yrpcResponse.getRequestId());
            }
        }else if(code == RespCode.SUCCESS_HEART_BEAT.getCode()){
            completableFuture.complete(null);
            log.error("已寻找到编号为{}的completableFuture，处理心跳检测，处理响应结果.",yrpcResponse.getRequestId(),yrpcResponse.getCode());
        }
    }
}
